<?php
namespace app\backend\controller;
use \think\Controller;
use think\Db;
use think\Request;
use app\backend\model\Permission as PermissionModel;
use app\backend\model\User as UserModel;
use think\Session;

class Index extends Controller
{
    public function index()
    {
    //    $res = Db::table('think_data')->select();
    //    $res = json_encode($res);
       return 'index';
    }

    public function Register(Request $request){

        $user = new UserModel;
        $user->inkey = md5($request->param('username'));
        $user_1 = UserModel::where('name', $request->param('username'))->find();
        // var_dump($user_1);
        if(!empty($user_1)){
            if($request->param('username') == $user_1->name){
                return 0;
            }
        }
        $user->name = $request->param('username');
        $user->gender = $request->param('gender');
        $user->avatar = $request->param('avatar');
        $user->password = $request->param('password');
        $user->email = $request->param('email');
        $user->intro = $request->param('intro');
        $user->date = $request->param('date');
        $user->save();
        $permission = PermissionModel::where('permission' ,'游览者')->find();
        $permission->num += 1;
        $permission->save();
        $user = UserModel::where('name', $request->param('username'))->find();
        Session::set('username', $request->param('username'), $request->param('username'));
        Session::set('avatar', $request->param('avatar'), $request->param('username'));        
        Session::set('permission', $user->permission->permission, $request->param('username'));
        Session::set('email', $request->param('email'), $request->param('username'));
        $userdata = [
            'username' => $request->param('username'),
            'avatar' => $request->param('avatar'),
            'permission' => $user->permission->permission
        ];
        return json_encode($userdata);
    }

    public function Login(Request $request){
        $username = $request->param('username');
        $user = UserModel::where('name', $username)->find();
        if(empty($user)){
            return 0;
        }
        if($user->password != $request->param('password')){
            return 0;
        }
        Session::set('username', $user->name);
        Session::set('avatar', $user->avatar);
        Session::set('permission', $user->permission->permission);
        $userdata = [
            'username' => $user->name,
            'avatar' => $user->avatar,
            'permission' => $user->permission->permission
        ];
        return json_encode($userdata);
    }
    public function LoginImage(Request $request){
        $username = $request->param('username');
        $user = UserModel::where('name', $username)->find();
        if(!empty($user)){
            return json_encode($user->avatar);
        }else{
            return 'https://www.fxpixels.site/tp5/public/uploads/20171227/25defd0ae403e5b922ff34eea115f053.jpeg';
        }
    }

    public function RegisterImage(){
        $file = request()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $filepath = 'https://www.fxpixels.site/tp5/public/uploads/'.$info->getSaveName();
                return $filepath;
            }else{
               return $file->getError();
            }
        }
    }

    public function KeepAlive(Request $request){
       $username = $request->param('username');
       if(Session::has('username')){
            $user = UserModel::where('name', $username)->find();
            $userdata = [
                'username' => $user->name,
                'avatar' => $user->avatar,
                'permission' => $user->permission->permission
            ];
            return json_encode($userdata);
       }else{
           return 0;
       }
    }
    public function LoginOut(Request $request){
        Session::clear();
        return 0;
    }
}